package com.mzx.javase.thread.daemon;

/**
 * 类描述：设置守护线程：守护线程指的是，A线程守护B线程执行，当B线程执行完毕进入到死亡状态时，A线程是否执行完毕都会进入到死亡状态
 *
 * @ClassName DaemonTest
 * @Description TODO
 * @Author 马子鑫
 * @Date 2022/8/3 17:34
 * @Version 1.0
 */
public class DaemonTest {
    public static void main(String[] args) {
        DaemonThread daemonThread = new DaemonThread();
        daemonThread.setName("守护线程");
        daemonThread.setDaemon(true);//设置此线程为守护线程
        daemonThread.start();
        //当主线程运行结束时，守护线程也随之进入死亡装填
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "第" + i);
        }
    }
}

class DaemonThread extends Thread {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "开始运行");
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName() + "运行到了：" + i);
        }
        System.out.println(Thread.currentThread().getName() + "运行结束");
    }
}
